|
|
|
|
|
|
|
stack pointer is returned (as a result of the pop function) and the stack pointer is moved to the previous location. |
|
|
|
|
|
|
|
|
All it takes to implement a stack is to allocate a block of memory and operate on it using the push and pop operations. Stacks are not often used in general purpose applications, thus many Visual Basic programmers are not familiar with them. But they are an essential part of every function or subroutine call. |
|
|
|
|
|
|
|
|
When an application is loaded, space is allocated in memory for a stack that will belong to the process. This is called the application stack or process stack. Your computer's CPU provides efficient hardware support for an application's stack. A CPU has a hardware register dedicated to work as a stack pointer that contains the location of the top data item on this stack. It also provides machine language push and pop instructions, along with instructions that can push and pop multiple items to and from a stack in a single operation. |
|
|
|
|
|
|
|
|
There are two other facts to keep in mind when dealing with your application's stack. First, every entry in the stack is 32 bits wide. If you want to place a smaller item on the stack, the extra space is unused. If you want to place a larger item on the stack, that item must span multiple 32-bit entries. Second, under x86 systems (486, Pentium, etc.) the stack grows downward. This means that, when you push an item on the stack, the value of the stack pointer decreases. When you pop an item, it increases. In other words, the stack is upside down compared to what you might intuitively expect. |
|
|
|
|
|
|
|
|
Why is it so important that the system stack be implemented efficiently in hardware? Not only is the application stack used every time a function is called, it is also used when passing parameters to functions and to allocate space for local variables. It is even sometimes used by functions to return results to the calling routine. |
|
|
|
|
|
|
|
|
Calling a Simple Function |
|
|
|
|
|
|
|
|
Consider a call to a function that has no parameters and no local variables. We'll define three of these functions, A, B, and C, as follows: |
|
|
|
|
|
|
|
|
Function A() As Long
End Function
Function B() As Long
Call A()
End Function
Sub Main()
Call B()
End Sub |
|
|
|
|
|